package com.cz.nongyetongv5.custom.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cz.nongyetongv5.R;

import java.util.List;

/**
 * Created by YeHan on 2017/6/28.
 * 自定义对话框
 */

public class CustomMyDialog extends Dialog {

    protected CustomMyDialog(Context context) {
        super(context);
    }

    public CustomMyDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;//标题
        private String message; //消息文字
        private String endDate; //结束种养时间
        private String singleButtonText;
        private String positiveButtonText;
        private String negativeButtonText;

        private OnClickListener singleButtonClickListener;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private OnClickListener endDateClickListener;

        private CustomMyDialog dialog;
        private View layout;
        private LayoutInflater inflater;

        private TextView tv_endDate;

        public Builder(Context context) {
            this.context = context;
            dialog = new CustomMyDialog(context, R.style.Dialogg);
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setMessage(List<String> messages) {
            String msg = "";
            for (int i = 0; i < messages.size(); i++) {
                int pos = i + 1;

                if (msg.equals("")) {
                    msg += pos + "、" + messages.get(i);
                } else {
                    msg += "\n" + pos + "、" + messages.get(i);
                }
            }

            this.message = msg;
            return this;
        }


        public Builder setEndDate(String endDate) {
            this.endDate = endDate;

            if (tv_endDate != null) {
                tv_endDate.setText(endDate);
            }
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setSingleButton(String singleButtonText, OnClickListener listener) {
            this.singleButtonText = singleButtonText;
            this.singleButtonClickListener = listener;
            return this;
        }

        public Builder setEndDateClickListenter(OnClickListener listener) {
            this.endDateClickListener = listener;
            return this;
        }

        /**
         * 从资源文件中设置消息
         *
         * @param message 资源文件ID
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }


        /**
         * 单一按钮对话框 确定
         *
         * @return
         */
        public CustomMyDialog createSingleButtonDialog() {
            layout = inflater.inflate(R.layout.custom_single_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            //设置消息文字
            ((TextView) layout.findViewById(R.id.message)).setText(message);
            //如果传入的按钮文字为空，则使用默认的“确定”
            if (singleButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_confirm)).setText(singleButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.tv_confirm)).setText("确定");
            }

            //设置点击事件
            (layout.findViewById(R.id.tv_confirm)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    singleButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });

            dialog.setContentView(layout);
            return dialog;
        }

        /**
         * 双按钮对话框，确定-取消
         *
         * @return
         */
        public CustomMyDialog createNormalDialog() {
            layout = inflater.inflate(R.layout.custom_normal_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            //设置消息文字
            ((TextView) layout.findViewById(R.id.message)).setText(message);

            //如果传入的按钮文字为空，则使用默认的“确定”和“取消”
            if (positiveButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_confirm)).setText(positiveButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.tv_confirm)).setText("确定");
            }

            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_cancel)).setText(negativeButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.tv_cancel)).setText("取消");
            }

            //设置点击事件
            (layout.findViewById(R.id.tv_confirm)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });
            (layout.findViewById(R.id.tv_cancel)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });

            dialog.setContentView(layout);
            return dialog;
        }

        /**
         * 结束种养对话框
         *
         * @return
         */
        public CustomMyDialog createEndPlantDialog() {
            layout = inflater.inflate(R.layout.custom_endplant_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            tv_endDate = (TextView) layout.findViewById(R.id.tv_date);

            //设置消息文字
            ((TextView) layout.findViewById(R.id.message)).setText(message);

            //如果传入的按钮文字为空，则使用默认的“确定”和“取消”
            if (positiveButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_confirm)).setText(positiveButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.tv_confirm)).setText("确定");
            }

            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_cancel)).setText(negativeButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.tv_cancel)).setText("取消");
            }

            //设置点击事件
            (layout.findViewById(R.id.tv_confirm)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });
            (layout.findViewById(R.id.tv_cancel)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });
            (layout.findViewById(R.id.tv_date)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    endDateClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });

            dialog.setContentView(layout);
            return dialog;
        }

        /**
         * 版本更新提示对话框
         * @return
         */
        public CustomMyDialog createUpdateVersionDialog() {
            layout = inflater.inflate(R.layout.custom_version_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            //设置消息文字
            ((TextView) layout.findViewById(R.id.message)).setText(message);
            ((TextView) layout.findViewById(R.id.tv_title)).setText(title);

            //如果传入的按钮文字为空，则使用默认的“确定”和“取消”
            if (positiveButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_confirm)).setText(positiveButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.tv_confirm)).setText("确定");
            }

            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_cancel)).setText(negativeButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.tv_cancel)).setText("取消");
            }

            //设置点击事件
            (layout.findViewById(R.id.tv_confirm)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });
            (layout.findViewById(R.id.tv_cancel)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });

            dialog.setContentView(layout);
            return dialog;
        }

    }
}
